﻿using AbpApplication.Expand;
using AbpApplication.InPut;
using AbpEntityFrameworkcore.DbContext;
using AbpToDomain.EntityInfo;
using AbpToDomain.View;
using LinqSharp;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace AbpApplication.AppServices
{
    public class ViewService : ApplicationService, IViewService
    {

        private readonly QueryDynamic_View_Materials queryDynamic_View_Materials;
        private readonly  QueryDynamic_View_NeetOutMaterial queryDynamic_View_NeetOutMaterial;
        private readonly QueryDynamic_View_Inlethouseline queryDynamic_View_Inlethouseline;
        private readonly QueryDynamic_View_TaskOrder queryDynamic_View_TaskOrder;
        private readonly QueryDynamic_View_Box queryDynamic_View_Box;
        private readonly  QueryDynamic_View_Log queryDynamic_View_Log;
        private readonly MyDbContext context;
        private readonly IRepository<View_User> View_user_res;
        private readonly IRepository<BoxGrid> BoxGrid_res;
        private readonly IRepository<Box> Box_res;



        public ViewService(QueryDynamic_View_Materials queryDynamic_View_Materials,
            IRepository<View_User> view_user_res,
            QueryDynamic_View_NeetOutMaterial queryDynamic_View_NeetOutMaterial,
            QueryDynamic_View_Inlethouseline queryDynamic_View_Inlethouseline,
            QueryDynamic_View_TaskOrder queryDynamic_View_TaskOrder,
            QueryDynamic_View_Box queryDynamic_View_Box,
            MyDbContext context,
            QueryDynamic_View_Log queryDynamic_View_Log,
            IRepository<BoxGrid> boxGrid_res,
            IRepository<Box> box_res)
        {
            this.queryDynamic_View_Materials = queryDynamic_View_Materials;
            View_user_res = view_user_res;
            this.queryDynamic_View_NeetOutMaterial = queryDynamic_View_NeetOutMaterial;
            this.queryDynamic_View_Inlethouseline = queryDynamic_View_Inlethouseline;
            this.queryDynamic_View_TaskOrder = queryDynamic_View_TaskOrder;
            this.queryDynamic_View_Box = queryDynamic_View_Box;
            this.context = context;
            this.queryDynamic_View_Log = queryDynamic_View_Log;
            BoxGrid_res = boxGrid_res;
            Box_res = box_res;
        }

        public async Task<ApiResponsePage> GetList_View_Box(InPut_ViewService5 InPut)
        {
            try
            {
                var view_Boxes = queryDynamic_View_Box.QueryDynameic(InPut.BoxCode,
                InPut.BoxGridQuantity,InPut.WarehouseLineCode);
                long count = view_Boxes.LongCount();//计算出了总条数
                long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
                view_Boxes = view_Boxes.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
                return new ApiResponsePage("获取成功", true, view_Boxes, count, pageCount);
            }
            catch (Exception ex)
            {
                Logger.LogWarning($"GetList_View_Box-{InPut}" + ex.Message);
                return new ApiResponsePage("内部服务器错误", false, null, null, null);
            }
        }//箱盒视图

        public async Task<ApiResponse> GetList_View_Box_Material(string BoxCode)//箱盒-物料视图
        {
            
           List<View_Box_Material> view_Box_Materials=  context.View_Box_Materials.Where(x=>x.BoxGridCode.Contains(BoxCode.Substring(0,10))).ToList();
           return new ApiResponse("获取成功",true,view_Box_Materials);
        }

        public async Task<ApiResponsePage> GetList_View_InlethouseLine(InPut_ViewService3 InPut)
        {
            try
            {
                var view_InlethouseLines = queryDynamic_View_Inlethouseline.QueryDynameic(InPut.InlethouseCode,
                 InPut.Xaxis, InPut.Yaxis, InPut.Zaxis,InPut.Type);
                long count = view_InlethouseLines.LongCount();//计算出了总条数
                long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
                view_InlethouseLines = view_InlethouseLines.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
                return new ApiResponsePage("获取成功", true, view_InlethouseLines, count, pageCount);
            }
            catch (Exception ex)
            {
                Logger.LogWarning($"GetList_View_InlethouseLine-{InPut}" + ex.Message);
                return new ApiResponsePage("内部服务器错误", false, null, null, null);
            }
        }//缓存仓视图

        public async Task<ApiResponsePage> GetList_View_Logs(InPut_ViewService6 InPut)//日志视图
        {
            try
            {
               var view_Logs = queryDynamic_View_Log.QueryDynameic(InPut.Type, InPut.User,InPut.Note,InPut.OrderNo);
                long count = view_Logs.LongCount();//计算出了总条数
                long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
                view_Logs = view_Logs.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
                return new ApiResponsePage("获取成功", true, view_Logs, count, pageCount);
            }
            catch (Exception ex)
            {
                Logger.LogWarning($"GetList_View_Logs-{InPut}" + ex.Message);
                return new ApiResponsePage("内部服务器错误", false, null, null, null);
            }
        }

        public async Task<ApiResponsePage> GetList_View_Materials(InPut_ViewService Input)
        {
            try
            {
                var view_materials = queryDynamic_View_Materials.QueryDynameic(Input.MaterialName,
                 Input.MaterialCode,Input.Batch,Input.Quantity,Input.BoxGridCode);
                long count = view_materials.LongCount();//计算出了总条数
                long pageCount = (long)Math.Ceiling(count * 1.0 / Input.PageSize);
                view_materials = view_materials.SelectPage(Input.PageIndex, Input.PageSize).ToList();
                return new ApiResponsePage("获取成功", true, view_materials, count, pageCount);
            }
            catch (Exception ex)
            {
                Logger.LogWarning($"GetList_View_Materials-{Input}" + ex.Message);
                return new ApiResponsePage("内部服务器错误", false, null, null, null);
            }
     
        }//物料视图

        public async Task<ApiResponsePage> GetList_View_NeetOutMaterial(InPut_ViewService2 InPut)
        {
            try
            {
                var view_neetoutmaterials = queryDynamic_View_NeetOutMaterial.QueryDynameic(InPut.MaterialName,
                 InPut.MaterialCode, InPut.Batch, InPut.BoxGridCode, InPut.OrderNo);
                long count = view_neetoutmaterials.LongCount();//计算出了总条数
                long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
                view_neetoutmaterials = view_neetoutmaterials.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
                return new ApiResponsePage("获取成功", true, view_neetoutmaterials, count, pageCount);
             }
            catch (Exception ex)
            {
                Logger.LogWarning($"GetList_View_NeetOutMaterial-{InPut}" + ex.Message);
                return new ApiResponsePage("内部服务器错误", false, null, null, null);
            }

        }//需要出库视图

        public async Task<ApiResponse> GetList_View_Proportion(InPut_ViewService8 InPut)
        {
             

            var boxs = await Box_res.WithDetailsAsync();
            boxs=boxs.Where(x=>x.WarehouseLine!=null);
            var res= boxs.Select(box=> new
            {
                boxCode=box.BoxCode,
                boxgrid=box.BoxGrids.Count(),
                Ratio = Math.Round((double)box.BoxGrids.Where(x => x.MaterialBases.Count > 0).Count() / box.BoxGridQuantity, 2),
            }).ToList();
            if (!string.IsNullOrEmpty( InPut.BoxCode))
            {
                res= res.Where(x=>x.boxCode.Contains(InPut. BoxCode)).ToList();
            }
            if (!string.IsNullOrEmpty(InPut.BoxGridCount))
            {
                res = res.Where(x => x.boxgrid==int.Parse( InPut.BoxGridCount)).ToList();
            }
            return new ApiResponse("获取成功",true, res);
        }//获取占比

        public async Task<ApiResponse> GetList_View_repestorder()//查看合并工单分组
        {
            List<View_repeatOrder> view_RepeatOrders = context.View_RepeatOrders.Where(x => string.IsNullOrEmpty(x.State)).ToList();
            var view_RepeatOrders_GroupBy = view_RepeatOrders.GroupBy(x => x.reelId).Where(x=>x.Count()>1).
                SelectMany((g, index) => g.Select(order => new
                {
                    GroupNumber = index + 1,
                    hSerial = order.hSerial,
                    moCode = order.moCode,
                    reelId = g.Key
                })) ;
           
            return new ApiResponse("获取成功", true, view_RepeatOrders_GroupBy);
        }//

        public async  Task<ApiResponse> GetList_View_SowingWarehouseLine()
        {
             var data=  await  context.SowingWallLines.ToListAsync();
            return new ApiResponse("获取成功", true, data);
        }

        public async Task<ApiResponsePage> GetList_View_TaskOrder(InPut_ViewService4 InPut)
        {
            try
            {
                var view_TaskOrders = queryDynamic_View_TaskOrder.QueryDynameic(InPut.BoxCode,InPut.Type,
                    InPut.OrderNo,InPut.State);
                long count = view_TaskOrders.LongCount();//计算出了总条数
                long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
                view_TaskOrders = view_TaskOrders.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
                return new ApiResponsePage("获取成功", true, view_TaskOrders, count, pageCount);
            }
            catch (Exception ex)
            {
                Logger.LogWarning($"GetList_View_NeetOutMaterial-{InPut}" + ex.Message);
                return new ApiResponsePage("内部服务器错误", false, null, null, null);
            }
        }//任务视图

        public async Task<ApiResponse> GetList_View_TaskOrder_OrderNo()//工单
        {
            try
            {
                List<View_TaskOrder_OrderNo> view_TaskOrder_OrderNos = context.View_TaskOrder_OrderNos.ToList();
                return new ApiResponse("获取成功", true, view_TaskOrder_OrderNos);
            }
            catch(Exception ex)
            {
                Logger.LogWarning($"GetList_View_TaskOrder_OrderNo-" + ex.Message);
                return new ApiResponse("内部服务器错误", false, null);
            }
   
        }

        public  async Task<ApiResponse>  GetList_View_User()
        {
            try
            {
                List<View_User> view_Users = await View_user_res.GetListAsync();
                return new ApiResponse("获取成功", true, view_Users);
            }
            catch(Exception ex)
            {
                Logger.LogWarning($"GetList_View_User" + ex.Message);
                return new ApiResponse("内部服务器错误", false, null);
            }
      
        }//用户视图

        public async Task<ApiResponse> QuaneryList_View_TaskOrder(InPut_ViewService7 InPut)
        {
            try
            {
                List<View_TaskOrder> view_TaskOrders = context.View_TaskOrders.Where(x=>InPut.OrderNos.Any(y=>y==x.OrderNo)&&x.State=="未下发").ToList();
                return new ApiResponse("获取成功", true, view_TaskOrders);
            }
            catch (Exception ex)
            {
                Logger.LogWarning($"QuaneryList_View_TaskOrder" + ex.Message);
                return new ApiResponse("内部服务器错误", false, null);
            }
         
        }

        public async Task<ApiResponse> SingleView_TaskOrder(string TaskOrderCode)
        {
            try
            {
                View_TaskOrder view_TaskOrder = await context.View_TaskOrders.FirstOrDefaultAsync(x => x.TaskOrderCode == TaskOrderCode);
                return new ApiResponse("获取成功", true, view_TaskOrder);
            }
            catch(Exception ex)
            {
                Logger.LogWarning($"SingleView_TaskOrder" + ex.Message);
                return new ApiResponse("内部服务器错误", false, null);
            }
      
        }
    }
}
